<?php include("../includes/header-member.php") ?>
<title>LazySwim</title>
<link rel="stylesheet" href="store.css" type="text/css" />
<script type="text/javascript">
<!--
function confirmEmpty() {
    var warning = "Are you sure you wish to completely empty your cart?";
	if (confirm(warning)){
		return true;
	}
	else{
		return false;
	}
}
//-->
</script>

</head>

<body>
<div id="page">
    <div id="page-top">
        <div id="page-bottom">
          <div id="header">
                <div id="header-info">
                    <h1>LAZYSWIM</h1>
                    <div class="description">Simplistic Swim System</div>
                </div>
				<!-- Header Goes Here -->
                <?php printHeader(); ?>
				<!-- Header Ends Here -->
                <div id="header-date">Today is <?php echo date('l, F jS, Y'); ?></div>
                <div id="header-cats">
				<!-- Welcome Message Starts Here -->
                    <ul>
					<?php include("../../includes/welcomeBar.php"); ?>
					</ul>
				<!-- Welcome Message Ends Here -->	
                </div>
            </div>
            <div id="main">
                <div id="sidebar">                 
                    <?php
					// include the corresponding navigation pages
                    include("shopSidebar.php");
                    include("../../includes/navigation.php");
					?>     
                </div>                     

            <!-- main body -->
            <?php
            require_once('cartFunctions.php'); 
            $user = $_SESSION['ls_userid'];
            $action = (isset($_POST['action']) && $_POST['action'] != '') ? $_POST['action'] : $_GET['action']; 
            
            if( $action == "add" ) {
                // add each selected item to the cart
                foreach( $_POST as $key => $value) {
                    $productID = $key;
                    $quantity = (int)$value;
                
                    // skip if value is empty or the action value
                    if( $productID != "" && $quantity != "" && $quantity != 0 ) {
                         addToCart($user, $productID, $quantity);
                    }
                }            
            }
            
            else if( $action == "delete" ) {
                    $productID = ( isset($_GET['productID']) ) ? $_GET['productID'] : 0;             
                    if( $productID == "all" ) {
                        removeCart($user); 
                    }
                    else if( $productID != 0) {
                         deleteFromCart($user, $productID);
                    }                        
            }
      
            $sql = "SELECT * FROM cart WHERE userID = '$user'";
            $items = mysql_query($sql) or die(mysql_error()); 
            ?>
            
            <h2>Your Cart</h2><br>
            
            <table cellpadding=2 cellpadding=2 width="65%">
            <tr><th>Product Name</th><th>Size</th><th>Price per unit</th>
            <th>Quantity Ordered</th><th>Subtotal</th>
            <th><a href="cart.php?action=delete&productID=all" onClick="return confirmEmpty()">Empty Cart</a></th></tr>
            <?php
            $total = 0.00;
            
            $i = 0;
            while($row = mysql_fetch_assoc($items)) { 
                // this is just to make the table pretty with alternating tr colours
                if($i % 2) { 
                    $class = 'row1'; 
                } 
                else { 
                    $class = 'row2'; 
                } 
                $i++;
                
                //fetch the product info
                $sql = "SELECT * FROM products WHERE productID = '$row[productID]'";
                $product = mysql_query($sql) or die(mysql_error()); 
                $product = mysql_fetch_assoc($product);
                
                $total += $row[quantity] * $product[price];                
                ?>
                <tr class="<?php echo $class; ?>"> 
                    <td align="center"><?php echo $product[name]; ?></td> 
                    <td align="center"><?php echo $product[size]; ?></td> 
                    <td align="center">$<?php echo $product[price]; ?></td> 
                    <td align="center"><?php echo $row[quantity]; ?></td>
                    <td align="center">$<?php echo sprintf("%01.2f", $row[quantity] * $product[price]); ?></td>
                    <td align="center"><a href="cart.php?action=delete&productID=<?php echo $row[productID]; ?>">Remove</a></td> 
                </tr> 
                
            <?php    
            }
            ?>
            <tr><th>Total Cost:</th><td colspan="3"></td><td align="center"><b>$<?php echo sprintf("%01.2f", $total); ?></b><td></tr>
            </table>
            <br><br>
            
            <?php                 
                //get a unique transactionID for the user combined with the currenttimestamp
                $transactionID = $user . time();
            ?>
            <form name="checkoutForm" 
                action="http://consort.cs.ualberta.ca/~c410/paybuddy/payment.cgi?grp=4&amt=<?php echo sprintf("%01.2f", $total); ?>&tx=<?php echo $transactionID; ?>&ret=http://www.hungmun.com/c410/site-gui/members/store/checkout.php" method="post"  target="_blank">
            <input type="submit" name="submit" value="Checkout">
            <!-- checkout, we don't need a confirm page because this page serves that purpose -->
            </form>
            
            <!-- end of main -->
            
            <div class="clear"></div>

            </div>
			<!-- Footer Goes Here -->
            <?php include("../../includes/footer.php"); ?>
			<!-- Footer Ends Here -->
        </div>
    </div>
</div>
</body>
</html>